<?php

class PostController extends Controller
{
	/**
	 * @var CActiveRecord the currently loaded data model instance.
	 */
	private $_model;

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new Post;
		if(isset($_POST['Post']))
		{
			$model->attributes=$_POST['Post'];
			if($model->save())
				$this->redirect('index');
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionUpdate()
	{
		$model=$this->loadModel();
		if(isset($_POST['Post']))
		{
			$model->attributes=$_POST['Post'];
			if($model->save())
				$this->redirect('index');
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * In ajax mode, the grid will be refreshed when the update is successful
	 * , while in normal mode, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$model = $this->loadModel($id);

		if(Yii::app()->request->isAjaxRequest)
		{
		    // Stop jQuery from re-initialization
			Yii::app()->clientScript->scriptMap['jquery.js'] = false;

			if(isset($_POST['action']) && $_POST['action'] == 'confirmDelete')
			{
				$model->delete();
				echo CJSON::encode( array(
					'status'=>'success',
					'content'=>'<div class="tips tips-ok">
				        <i class="ico-ok"></i>
				        <div class="cont">
				            <h4>Info</h4>
				            <p>Deleted succussfully.</p>
				        </div>
				    </div>',
					));
				exit;
			}
			else if(isset($_POST['action']))
			{
				echo CJSON::encode( array(
					'status'=>'canceled',
					'content'=>'<div class="tips tips-info">
				        <i class="ico-info"></i>
				        <div class="cont">
				            <h4>Info</h4>
				            <p>Deleted canceled.</p>
				        </div>
				    </div>',
					));
				exit;
			}
			else
			{
				echo CJSON::encode( array(
					'status'=>'failure',
					'content'=>$this->renderPartial('delete', array(
						'model'=>$model ), true, true),
					));
				exit;
			}
		}
		else
		{
			if(isset($_POST['confirmDelete']))
			{
				$model->delete();
				$this->redirect('index');
			}
			else if(isset($_POST['denyDelete']))
				$this->redirect('index');
			else
				$this->render('delete', array('model'=>$model));
		}
	}

	/**
	 * Lists all models.
	 */
	/*public function actionIndex()
	{
		$criteria=new CDbCriteria(array(
			'condition'=>'status='.Post::STATUS_PUBLISHED,
			'order'=>'update_time DESC',
			'with'=>'commentCount',
		));
		if(isset($_GET['tag']))
			$criteria->addSearchCondition('tags',$_GET['tag']);

		$dataProvider=new CActiveDataProvider('Post', array(
			'pagination'=>array(
				'pageSize'=>Yii::app()->params['postsPerPage'],
			),
			'criteria'=>$criteria,
		));

		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}*/

	/**
	 * Manages all models.
	 */
	public function actionIndex()
	{
		$model=new Post('search');
		if(isset($_GET['Post']))
			$model->attributes=$_GET['Post'];
		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Suggests tags based on the current user input.
	 * This is called via AJAX when the user is entering the tags input.
	 */
	public function actionSuggestTags()
	{
		if(isset($_GET['q']) && ($keyword=trim($_GET['q']))!=='')
		{
			$tags=Tag::model()->suggestTags($keyword);
			if($tags!==array())
				echo implode("\n",$tags);
		}
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 */
	public function loadModel()
	{
		if($this->_model===null)
		{
			if(isset($_GET['id']))
			{
				if(Yii::app()->user->isGuest)
					$condition='status='.Post::STATUS_PUBLISHED.' OR status='.Post::STATUS_ARCHIVED;
				else
					$condition='';
				$this->_model=Post::model()->findByPk($_GET['id'], $condition);
			}
			if($this->_model===null)
				throw new CHttpException(404,'The requested page does not exist.');
		}
		return $this->_model;
	}

}
